package com.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.dto.PageDTO;
import com.domain.dto.user.AddUserDTO;
import com.domain.dto.user.ModifyUserDTO;
import com.domain.dto.user.ModifyUserRoleDTO;
import com.domain.dto.user.UserDTO;
import com.domain.query.user.UserQuery;
import com.domain.vo.JsonVO;
import com.rbac.mapper.UserMapper;
import com.rbac.pojo.User;
import com.rbac.service.IUserService;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper(componentModel = "spring")
interface msUserMapper{
    User addUserToUser(AddUserDTO addUserDTO);
    User modifyUserToUser(ModifyUserDTO modifyUserDTO);
    UserDTO userToUserDTO(User user);
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private msUserMapper msUserMapper;

    @Override
    public PageDTO<UserDTO> queryUserList(UserQuery userQuery) {
        System.out.println(userQuery);
        Page<User> page = new Page<>(userQuery.getPageIndex(), userQuery.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<User>();

        if(userQuery.getUserId() != null){
            wrapper.eq("user_id",userQuery.getUserId());
        }

        Page<User> result = baseMapper.selectPage(page, wrapper);
        System.out.println(result.getRecords());

        return PageDTO.create(result, msUserMapper::userToUserDTO);
    }

    @Override
    public UserDTO queryUserByUserId(Integer UserId) {
        return msUserMapper.userToUserDTO(baseMapper.selectById(UserId));
    }

    //添加角色
    @Override
    public Integer addUser(AddUserDTO addUserDTO) {
        //将dto数据转换成do数据
        User user = msUserMapper.addUserToUser(addUserDTO);
        //添加用户到用户表
        int addedRow = baseMapper.insert(user);
        //维护用户角色中间表
        //TODO 枚举类，1为普通用户
        int row = this.addRoleUser(user.getUserId(), 1);
        return addedRow;
    }

    @Override
    public Integer modifyUser(ModifyUserDTO modifyUserDTO) {
        //将dto数据转换成do数据
        User modifyUser = msUserMapper.modifyUserToUser(modifyUserDTO);
        //修改数据并返回修改的行数
        return baseMapper.updateById(modifyUser);
    }

    @Override
    public Integer removeUser(List<Integer> userIds) {
        int totalDeletedRow = 0;
        for(Integer userId : userIds){
            //删除用户角色中间表数据
            this.removeRoleUser(userId);
            //删除用户数据
            int deletedRow = baseMapper.deleteById(userId);
            totalDeletedRow += deletedRow;
        }
        //返回删除的总行数
        return totalDeletedRow;
    }

    @Override
    public Integer modifyRoleUser(ModifyUserRoleDTO modifyUserRoleDTO){
        Integer userId = modifyUserRoleDTO.getUserId();
        List<Integer> roleIds = modifyUserRoleDTO.getRoleIds();

        //删除userId用户的全部权限
        int deletedRow = this.removeRoleUser(userId);
        //新增userId的对应权限列表
        int totalAddedRow = 0;
        for(Integer roleId : roleIds){
            int addedRow = this.addRoleUser(userId, roleId);
            totalAddedRow +=addedRow;
        }
        return totalAddedRow;
    }

    @Override
    public List<String> queryRoleNameListByUserId(Integer userId) {
        return baseMapper.queryRoleNameListByUserId(userId);
    }


    //以下为user表操作role_user中间关联表的数据方法

    //TODO 1、添加用户权限中间表
    public Integer addRoleUser(Integer userId, Integer roleId){
        return baseMapper.addRoleUser(userId, roleId);
    }

    //TODO 2、查询用户userId对应的角色列表
    public List<String> queryRoleUserByUserId(Integer userId){
        return baseMapper.queryRoleUserByUserId(userId);
    }

    //TODO 3、删除用户角色中间表数据
    public Integer removeRoleUser(Integer userId){
        return baseMapper.removeRoleUser(userId);
    }

    //TODO 4、修改用户权限

}
